{% extends 'backend/base_backend.html' %}

{% block css %}
    {% load static %}
    <link href="{% static 'js/sweetalert/sweetalert.css' %}" rel="stylesheet"></link>
    <script charset="utf-8" src="{% static 'js/sweetalert/sweetalert.min.js' %}"></script>
    {#    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>#}
{% endblock %}

{% block article %}
    <!--接收当前用户所有文章-->
    <table class="table table-hover table-striped">
        <thead>
        <tr>
            <th>标题</th>
            <th>评论数</th>
            <th>点赞数</th>
            <th>操作</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {% for article in page_queryset %}
            <tr>
                <td><a href="/{{ request.user.username }}/article/{{ article.pk }}/">{{ article.title }}</a></td>
                <td>{{ article.comment_num }}</td>
                <td>{{ article.up_num }}</td>
                <td>
                    {#                    <button class="btn btn-info">编辑</button>#}
                    <a style="cursor:pointer;" class="test">编辑</a>
                </td>

                <td>
                    <a style="cursor:pointer;" class="del_article" article_id="{{ article.pk }}">删除</a>

                    {#                    <button#}
                    {#                            class="btn btn-danger del_article"#}
                    {#                            article_id="{{ article.pk }}">#}
                    {#                        删除#}
                    {#                    </button>#}
                </td>
            </tr>
        {% endfor %}

        </tbody>
    </table>
    <div class="pull-right">
        {{ page_obj.page_html|safe }}
    </div>

{% endblock %}

{% block js %}
    {% load static %}

    <script>


        $(".del_article").click(function () {
            let article_id = $(this).attr('article_id');
            // console.log(article_id)
            swal({
                    title: "你确定删除吗？",
                    text: "考虑清楚了吗，可能需要跑路",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonClass: "btn-danger",
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    closeOnConfirm: false,
                    closeOnCancel: false,
                    dangerMode: true,
                },
                function (isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            url: '',
                            type: 'post',
                            data: {
                                "article_id": article_id,
                                "csrfmiddlewaretoken": '{{csrf_token}}',
                            },

                            success: function (args) {
                                if (args.code == 1000) {
                                    swal({
                                            title: args.msg,
                                            text: "赶紧跑路吧",
                                            type: "success",
                                        },
                                        function (isdelete) {
                                            if (isdelete) {
                                                window.location.reload()
                                            }
                                        })
                                }
                            }
                        })
                    } else {
                        //swal("ok", "明智的决定", "info",);
                        swal.close();
                    }

                })
        })
    </script>

{% endblock %}